/**
 * 对bittool.h的测试。
 */
#include <stdio.h>
#include <stdlib.h>

#include "test_tools.h"

static void power_of_two_test(void)
{
    int power_of_two[31] = {
        1, 2, 4, 8, 16, 32, 64, 128,
        256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
        65536, 131072, 262144, 524288,
        1048576, 2097152, 4194304, 8388608,
        16777216, 33554432, 67108864, 134217728,
        268435456, 536870912, 1073741824
    };
    int power_of_two_len =  \
        sizeof(power_of_two) / sizeof(typeof(int));
    int power_of_two_neg[32] = {
        40274827, 45109751, 71599454, 145528397,
        172640318, 181668092, 206694668, 228689435,
        229143103, 370913251, 415683552, 440043530,
        449468073, 486752797, 502173330, 517981766,
        529880509, 0, 547086656, 673811464,
        747152806, 849726198, 849999310, 850935368,
        894060854, 941654467, 956201283, 980192530,
        1040876919, 1050177929, 1053581675, 1068831827
    };
    int power_of_two_neg_len =  \
        sizeof(power_of_two_neg) / sizeof(typeof(int));
    int cnt = 0;

    /// is_power_of_two的正例测试
    for (cnt = 0; cnt < power_of_two_len; cnt++) {
        if (!is_power_of_two(power_of_two[cnt])) {
            break;
        }
    }
    if (cnt == power_of_two_len) {
        LOG_TEST_PASS("is_power_of_two(possitive) sucess!\n");
    } else {
        LOG_TEST_FAIL("is_power_of_two(possitive) failed at case %d!"
            " %d is the power of 2!\n",
            cnt,
            power_of_two[cnt]);
    }

    /// is_power_of_two的反例测试
    for (cnt = 0; cnt < power_of_two_neg_len; cnt++) {
        if (is_power_of_two(power_of_two_neg[cnt])) {
            break;
        }
    }
    if (cnt == power_of_two_neg_len) {
        LOG_TEST_PASS("is_power_of_two(negative) sucess!\n");
    } else {
        LOG_TEST_FAIL("is_power_of_two(negative) failed at case %d!"
            " %d is the power of 2!\n",
            cnt,
            power_of_two_neg[cnt]);
    }
}

void bittool_tests(void)
{
    printf("\n* bittool_test:\n");
    power_of_two_test();
}